package com.util;

import java.sql.*;

public class DBUtil {
    public static Connection getConnection(){
        //注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取连接，输入账号密码
        Connection con=null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wenote?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT", "root", "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    //关闭连接
    public static void close(Connection con, PreparedStatement ps, ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
